#-*-coding:utf-8-*-
import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as signal
from pylab import mpl


fs=1500
ysc=0.2
x=np.arange(0,ysc,ysc/(ysc*fs))


def py_init(jidai_1,sin1_hz,zxb1_hz):
	y_sin1=10*np.sin(2*sin1_hz*np.pi*x)
	a=0.5*np.sin(2*zxb1_hz*np.pi*x)
	y_zxb1=[]
	for i in a:
		if i>0:
			y_zxb1.append(0.5+jidai_1)
		else:
			y_zxb1.append(-0.5+jidai_1)
	x_1=np.multiply(y_sin1,y_zxb1)
	return y_zxb1,x_1
def py2_init(sin_hz,sin_v,Fstop1,Fstop2):
	a=sin_v*np.sin(2*sin_hz*np.pi*x)
	a_1=fda(a,Fstop1,Fstop2)
	return a_1

def fda(x_1,Fstop1,Fstop2):
	b, a = signal.butter(8, [2.0*Fstop1/fs,2.0*Fstop2/fs], 'bandpass')
	filtedData = signal.filtfilt(b,a,x_1)
	return filtedData

def fda2(x_1,Fc):
	b, a = signal.butter(8,Fc/fs, 'lowpass')  
	filtedData = signal.filtfilt(b, a,x_1) 
	return filtedData

def hz_clip(xs):
	fft_size=ysc*fs
	xf = np.fft.rfft(xs)/fft_size
	freqs = np.linspace(0, fs/2, fft_size/2+1)
	xfp = 20*np.log10(np.clip(np.abs(xf), 1e-20, 1e100))
	return freqs, xfp
	
#plt.xlim(-1, 2)


y_10,y_1=py_init(1.1,100,10)
y_20,y_2=py_init(1.1,300,10)

y_3=py2_init(100,10,50,150)
y_4=py2_init(300,10,200,490)

y_add=y_1+y_2

y_add_1=fda(y_add,50,250)
y_add_2=fda(y_add,200,490)

y_mul_1=np.multiply(y_add_1,y_3)
y_mul_2=np.multiply(y_add_2,y_4)

y_get_1=fda2(y_mul_1,100.0)

y_get_2=fda2(y_mul_2,300.0)



#hz_clip(y_mul_2)

plt.subplot(311)
d1,d2=hz_clip(y_add_2)
plt.plot(d1,d2)

plt.subplot(312)
d1,d2=hz_clip(y_mul_2)
plt.plot(d1,d2)

plt.subplot(313)
d1,d2=hz_clip(y_get_2)
plt.plot(d1,d2)

plt.grid()
plt.show()

'''
plt.subplot(211)
plt.title(u'100hz')
plt.plot(x,y_get_1)

plt.subplot(212)
plt.title('300hz')
plt.plot(x,y_get_2)

'''
'''
plt.subplot(261)
plt.title('jidaixinhao 1')
plt.plot(x,y_10)

plt.subplot(262)
plt.title('jidai cheng 1')
plt.plot(x,y_1)

plt.subplot(263)
plt.title('xiang jia')
plt.plot(x,y_add)

plt.subplot(264)
plt.title('dai tong 1')
plt.plot(x,y_add_1)

plt.subplot(265)
plt.title('xiang cheng 1')
plt.plot(x,y_mul_1)

plt.subplot(266)
plt.title('di tong 1')
plt.plot(x,y_get_1)

plt.subplot(267)
plt.title('jidaixinhao 2')
plt.plot(x,y_20)

plt.subplot(268)
plt.title('jidai cheng 2')
plt.plot(x,y_2)

plt.subplot(269)
plt.title('xiang jia')
plt.plot(x,y_add)

plt.subplot(2,6,10)
plt.title('dai tong 2')
plt.plot(x,y_add_2)

plt.subplot(2,6,11)
plt.title('xiang cheng 1')
plt.plot(x,y_mul_2)

plt.subplot(2,6,12)
plt.title('di tong 2')
plt.plot(x,y_get_2)


plt.grid()
plt.show()
'''